package com.newlink.building.userinfo.mvp

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent
import com.newlink.building.common_base.base.BasePresenter
import com.newlink.building.common_base.ext.ss
import com.newlink.building.common_base.model.bean.DeviceListRequest
import com.newlink.building.common_base.utils.NLog
import com.newlink.building.userinfo.mvp.contract.Module_UserInfo_AuthParamContract
import com.newlink.building.userinfo.mvp.model.Module_UserInfo_AuthParamModel

/**
 * @author wangyang
 * @date 2022/9/20
 * @description 我的-关于页面的Presenter
 */
class Module_UserInfo_AuthParamPresenter : BasePresenter<Module_UserInfo_AuthParamContract.Model, Module_UserInfo_AuthParamContract.View>(),
    Module_UserInfo_AuthParamContract.Presenter {

    private var isResume = false

    override fun createModel(): Module_UserInfo_AuthParamContract.Model? = Module_UserInfo_AuthParamModel()

    override fun getAvailableArea(request: DeviceListRequest) {
        NLog.e("[jake]..........getAvailableArea. $request")
        mModel?.getAvailableArea(request)?.ss(mModel, mView) { result ->
            if (result?.data != null && isResume) {
                result.data?.list.let {
                    if (it != null) {
                        mView?.refreshDeviceList(result.data.total, it)
                    }
                }
            }
            mView?.hideLoading()
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    fun onResume(owner: LifecycleOwner) {
        isResume = true
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    open fun onPause(owner: LifecycleOwner) {
        isResume = false
    }

    override fun getFamilyMember(token: String) {
        mModel?.getFamilyMembers(token)?.ss(mModel, mView) { result ->
            if (result?.code == 200) {
                mView?.onGetFamilyMemberSuccess(result?.data)
            } else {
                val msg = result?.msg
                mView?.onGetFamilyMemberFailed(msg!!)
            }
        }

    }
}